package C7_5_CompositeReplaceImplicitTree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author xufy
 * @version 1.0
 **/
public class TagNode {

    private String name = "";
    private String value = "";
    private StringBuffer attributes;

    private List<Object> children;



    public TagNode(String name) {
        this.name = name;
        attributes = new StringBuffer("");
    }

    public void addAttribute(String attribute, String value) {
        attributes.append("");
        attributes.append(attribute);
        attributes.append("='");
        attributes.append(value);
        attributes.append("'");
    }

    public void addValue(String value) {
        this.value = value;
    }

//    @Override
//    public String toString() {
//        String result;
//        result = "<" + name + attributes + ">" +
//                value +
//                "</" + name + ">";
//        return result;
//    }
    @Override
    public String toString() {
        String result;
        result="<"+name+attributes+">";
        Iterator it=children().iterator();
        while (it.hasNext()){
            TagNode node = (TagNode) it.next();
            result+=node.toString();
        }
        result+=value;
        result+="</"+name+">";
        return result;
    }
    private List children(){
        if(children ==null)
            children =new ArrayList();
        return children;
    }
    public void add(TagNode child){
        children().add(child);

    }
}
